<?php
/**
 * @package		Joomla.Site
 * @subpackage	mod_menu
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access.
defined('_JEXEC') or die;
if ($menutype == 'mainmenu'){
	$db					=& JFactory::getDBO();
// Note. It is important to remove spaces between elements.
$itemid = JRequest::getVar( 'Itemid' );
//for($i<sizeof($list);$i++){
$i=0;
foreach ($list as $aitem){
	$class = '';
	if (!$itemid && $i == 0) {
		$class = 'class="current"';
	} 
	if ($itemid == $aitem->id) {
		$class = 'class="current"';
	}
	$mainlink = (!empty($aitem->flink) || $aitem->type != 'separator') ? "href='".$aitem->flink."'" : '';
?>

<ul class="dropdown">
  <li <?php echo $class;?>><a style="cursor: pointer" <?php echo $mainlink?>><span><?php echo $aitem->title?></span></a>
    <?php 
	$submenu = modMenuHelper::getListMenuByID($aitem->id, $menutype);
	//$qs = "SELECT * FROM `#__menu` WHERE menutype = 'mainmenu' AND published = 1 AND parent_id = ".$aitem->id;	
	//$db->setQuery( $qs );
	//$submenu = $db->loadObjectList();
	//echo $aitem->id;
	//echo sizeof($list);
	/*echo '<pre>';
	print_r($submenu);*/
	if ($submenu){
	?>
    <ul class="children" style="margin-top: -1px;">
      <?php for($s=0;$s<sizeof($submenu);$s++){
				$subclass = ($s == sizeof($submenu)-1) ? 'class="last"' : "";
				$sublink = (!empty($submenu[$s]->flink) || $submenu[$s]->type != 'separator') ? "href='".$submenu[$s]->flink."'" : '';
				if (!empty($sublink)){
					$sublinkclass = ''; 
					if ($itemid == $submenu[$s]->id) {
						$sublinkclass = 'class="current"';
					}
				?>
					<li <?php echo $subclass?>><a <?php echo $sublinkclass.$sublink; ?>><?php echo $submenu[$s]->title?></a></li>
			<?php 
				}else{ ?>
				  <li class="separator">
					<div><?php echo $submenu[$s]->title?></div>
				  </li>
		  	<?php
            	}
			}
		?>
    </ul>
    <?php 
	}
	?>
  </li>
</ul>
<?php $i++;
} //exit();
}elseif($menutype == 'footmenu'){
	for($f=0;$f<sizeof($list);$f++){
		$flink = (!empty($list[$f]->flink) || $list[$f]->type != 'separator') ? "href='".$list[$f]->flink."'" : '';		
		?>
<a href="#"><a style="cursor: pointer;" <?php echo $flink?>><?php echo $list[$f]->title?></a>
<?php 
		if ($f < sizeof($list)-1) echo "<span>|</span>";
	}
}else{
?>
<ul class="menu<?php echo $class_sfx;?>"<?php
	$tag = '';
	if ($params->get('tag_id')!=NULL) {
		$tag = $params->get('tag_id').'';
		echo ' id="'.$tag.'"';
	}
?>>
  <?php
foreach ($list as $i => &$item) :
	$class = 'item-'.$item->id;
	if ($item->id == $active_id) {
		$class .= ' current';
	}

	if (in_array($item->id, $path)) {
		$class .= ' active';
	}
	elseif ($item->type == 'alias') {
		$aliasToId = $item->params->get('aliasoptions');
		if (count($path) > 0 && $aliasToId == $path[count($path)-1]) {
			$class .= ' active';
		}
		elseif (in_array($aliasToId, $path)) {
			$class .= ' alias-parent-active';
		}
	}

	if ($item->deeper) {
		$class .= ' deeper';
	}

	if ($item->parent) {
		$class .= ' parent';
	}

	if (!empty($class)) {
		$class = ' class="'.trim($class) .'"';
	}

	echo '<li'.$class.'>';

	// Render the menu item.
	switch ($item->type) :
		case 'separator':
		case 'url':
		case 'component':
			require JModuleHelper::getLayoutPath('mod_menu', 'default_'.$item->type);
			break;

		default:
			require JModuleHelper::getLayoutPath('mod_menu', 'default_url');
			break;
	endswitch;

	// The next item is deeper.
	if ($item->deeper) {
		echo '<ul>';
	}
	// The next item is shallower.
	elseif ($item->shallower) {
		echo '</li>';
		echo str_repeat('</ul></li>', $item->level_diff);
	}
	// The next item is on the same level.
	else {
		echo '</li>';
	}
endforeach;
}
?>
</ul>
